package com.song.studylineserver.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import com.song.studylineserver.domain.Title;
import com.song.studylineserver.domain.User;
import com.song.studylineserver.service.TitleService;
import com.song.studylineserver.service.UserService;
import com.song.studylineserver.service.TopicService;
import com.song.studylineserver.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;

@Api(tags = "教学文章")
@RestController
@RequestMapping("/study/")
public class TitleController {

    @Resource
    TitleService titleService;

    @Autowired
    TopicService topicService;

    @Autowired
    UserService userService;

    @ApiOperation("test")
    @GetMapping(value = "getAll/{type}")
    public Result getAllByTitle(@PathVariable String type)
    {
        return new Result(200,"ok",titleService.selectTitleByType(type));
    }


    @ApiOperation("getTitle")
    @RequestMapping(value = "getTitle/{id}")
    public  Result getTitleById(@PathVariable Integer id){
        return new Result(200,"获取成功",titleService.selectTitleById(id));
    }

    @ApiOperation("教师信息")
    @RequestMapping(value = "getTeacherInfo")
    public  Result getTeacherInfo(){
        Result data = Result.ok();
        String id  =Convert.toStr(StpUtil.getLoginId());
        data.put("essay",titleService.selectTitleByUser(id));
data.put("title",topicService.searchAllByTopicAuthor(Convert.toLong(StpUtil.getLoginId())));
        return data;
    }


    @ApiOperation("添加文章")
    @RequestMapping(value = "/add")
    public  Result addTitle(@RequestBody Title body){

        User user = userService.getById((Serializable) StpUtil.getLoginId());

        //检查有没有这篇文章
        if(titleService.selectTitleById(body.getTitleId())!=null)
        {

        }

        body.setTitleAuthor(Convert.toStr(user.getUserId()));
        body.setTitleCreatetime(DateTime.now());
        return new Result(200,"添加成功",titleService.saveOrUpdate(body));
    }

}
